<template>
  <div class="searchbox">
    <div class="header">
      <div class="flexbox">
        <div class="textbox">
          <i class="iconfont icon-sousuo"></i>
          <input type="search" placeholder="搜索音乐、视频、播客、歌词" v-model="text" @keypress.13="tosearchresult" />
        </div>
        <div class="cancelbox" @click.stop="returnmain">取消</div>
      </div>
    </div>
    <div class="hot" v-show="text==''">
      <div class="title">
        <div>热搜榜</div>
      </div>
      <div :class="['content',{'less':less}]">
        <div :class="['more',{'show':less}]" @click.stop="showmorelist">
          展开更多
          <i class="iconfont icon-xiangxia"></i>
        </div>
        <div
          :class="['detail',{'less':less&&(index==8||index==9)}]"
          v-for="(ele,index) in hotserchlist"
          :key="ele.score"
          @click.stop="hotsearch(ele.searchWord)"
        >
          <div class="number">{{index+1}}</div>
          <div>{{ele.searchWord}}</div>
        </div>
      </div>
    </div>
    <div class="suggest" v-show="text!=''">
      <ul>
        <li v-for="(ele,index) in suggest" :key="index" @click.stop="hotsearch(ele.keyword)">
          <i class="iconfont icon-sousuo"></i>
          <div>{{ele.keyword}}</div>
        </li>
        <li v-show="suggest==undefined" class="none">搜索&nbsp;“{{text}}”</li>
      </ul>
    </div>
  </div>
</template>
  
  <script>
import { searchHot } from "../assets/api/search.js";
import { searchSuggest } from "../assets/api/search.js";
export default {
  data() {
    return {
      hotserchlist: [],
      less: true,
      suggest: [],
      text: "",
    };
  },
  methods: {
    returnmain() {
      this.$emit("returnmainson");
    },
    hotsearch(text) {
      this.text = text;
      this.tosearchresult();
    },
    showmorelist() {
      this.less = false;
    },
    tosearchresult() {
      this.$router.push({
        name: "searchresult",
        params: { result: this.text },
      });
    },
  },
  watch: {
    text(newV) {
      searchSuggest({ keywords: newV, type: "mobile" }).then((data) => {
        if (data.result) {
          this.suggest = data.result.allMatch;
        }
      });
    },
  },
  created() {
    searchHot().then((data) => {
      this.hotserchlist = data.data;
    });
  },
  beforeDestroy() {
    this.less = true;
  },
};
</script>
  
  <style lang="less">
.searchbox {
  width: 100%;
  height: 100%;
  color: rgba(255, 255, 255, 0.9);
  position: fixed;
  left: 0;
  top: 0;
  .header {
    background-color: rgba(0, 0, 0, 0.01);
    height: 88px;
    backdrop-filter: blur(20px);
    background-blend-mode: multiply;
    box-sizing: border-box;
    padding: 49px 15px 0 15px;
    padding-bottom: 5px;
    .flexbox {
      display: flex;
      align-items: center;
      .textbox {
        margin-right: 14px;
        flex: 1;
        position: relative;
        i {
          color: rgba(255, 255, 255, 0.6);
          position: absolute;
          margin: auto;
          top: 0;
          bottom: 0;
          width: 14px;
          height: 14px;
          font-size: 14px;
          left: 14px;
        }
        input {
          box-sizing: border-box;
          width: 100%;
          border: none;
          border-radius: 10000px;
          background-color: rgba(255, 255, 255, 0.135);
          height: 35px;
          padding: 0px 36px;
          line-height: 35px;
          &::-webkit-search-cancel-button {
            display: none;
          }
          &::-webkit-input-placeholder {
            color: rgba(255, 255, 255, 0.3);
          }
        }
      }
      .cancelbox {
        font-size: 17px;
      }
    }
  }
  .hot {
    margin-top: 17px;
    padding: 0 15px;
    .title {
      font-size: 14px;
      font-weight: lighter;
    }
    .content {
      width: 100%;
      background-color: rgba(255, 255, 255, 0.08);
      border-radius: 8px;
      padding: 4px 16px 22px;
      box-sizing: border-box;
      margin-top: 14px;
      display: flex;
      flex-wrap: wrap;
      height: 360px;
      overflow: hidden;
      position: relative;
      transition: all 0.3s ease-in-out;
      &.less {
        height: 225px;
      }
      .more {
        display: none;
        text-align: center;
        position: absolute;
        margin: auto;
        bottom: 16px;
        left: 0;
        right: 0;
        color: rgba(255, 255, 255, 0.5);
        &.show {
          display: inline-block;
        }
      }
      .detail {
        width: 50%;
        display: flex;
        font-weight: lighter;
        height: 16px;
        font-size: 14px;
        margin-top: 18px;
        color: white;
        &.less {
          margin-bottom: 50px;
        }
        .number {
          margin-right: 14px;
          color: rgba(255, 255, 255, 0.5);
        }
      }
    }
  }
  .suggest {
    ul {
      li {
        display: flex;
        height: 41px;
        line-height: 41px;
        padding: 0 15px;
        font-size: 14px;
        &.none {
          color: #8baccb;
        }
        i {
          color: rgba(255, 255, 255, 0.5);
          margin-right: 8px;
          font-size: 14px;
        }
        div {
          flex: 1;
          border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
      }
    }
  }
}
</style>